﻿// DlgSetTray.cpp: 实现文件
//

#include "stdafx.h"
#include "DlgSetTray.h"
#include "afxdialogex.h"


// DlgSetTray 对话框

IMPLEMENT_DYNAMIC(DlgSetTray, CDialogEx)

DlgSetTray::DlgSetTray(StorePoint* psp, CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SETTRAY, pParent)
	, m_psp(psp)
	, m_nSpacing(0)
	, m_nZNumber(0)
	, m_nPreviousSel(-1)
	, m_strCode(_T(""))
{

}

DlgSetTray::~DlgSetTray()
{
}

void DlgSetTray::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_cbTrayNumber);
	DDX_Text(pDX, IDC_EDIT1, m_nSpacing);
	DDV_MinMaxInt(pDX, m_nSpacing, 50, 10000);
	DDX_Text(pDX, IDC_EDIT2, m_nZNumber);
	DDV_MinMaxInt(pDX, m_nZNumber, 1, 10);
	DDX_Text(pDX, IDC_EDIT3, m_strCode);
}


BEGIN_MESSAGE_MAP(DlgSetTray, CDialogEx)
	ON_CBN_SELCHANGE(IDC_COMBO1, &DlgSetTray::OnCbnSelchangeCombo1)
	ON_BN_CLICKED(IDOK, &DlgSetTray::OnBnClickedOk)
END_MESSAGE_MAP()


// DlgSetTray 消息处理程序


BOOL DlgSetTray::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	CArray< Tray::Record> & arr = theApp.m_tray.m_arrRecord;
	m_arrTrayRecord.SetSize(0, 10);	// 预留10个，避免内存碎片
	INT_PTR j = -1;
	for (INT_PTR i = 0, len = arr.GetCount(); i < len; i++)
	{
		Tray::Record& record = arr[i];
		
		// 如果托类型编号相同
		//if (record.nNumber == j)
		//{
		//	// 并且区号相同，两个16位字符变成一个32位整数
		//	if(*((PINT32)m_psp->m_bufCode) == *((PINT32)record.szCode))
		//		m_arrTrayRecord[j] = record;
		//	// 否则跳过
		//}
		//else
		{
			m_arrTrayRecord.Add(record);	// 按顺序插入，或者加到最后
			j++;
		}
	}

	INT_PTR len = m_arrTrayRecord.GetCount();
	if (!len)
	{
		AfxMessageBox(L"没有货托数据");
		EndDialog(IDCANCEL);
		return FALSE;
	}

	// 加到组合框中
	INT_PTR sel = -1;
	for (INT_PTR i = 0; i < len; i++)
	{
		Tray::Record& record = m_arrTrayRecord[i];

		// 要求m_arrTrayRecord中的nNumber必须连续
		if (record.nNumber == m_psp->m_nTray)
			sel = i;

		CString str;
		str.Format(L"% 3i %s", record.nNumber, (LPCTSTR)record.m_strNote);

		m_cbTrayNumber.AddString(str);
	}
	if(sel >= 0)
		m_cbTrayNumber.SetCurSel((int)sel);
	OnCbnSelchangeCombo1();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

// 托号改变
void DlgSetTray::OnCbnSelchangeCombo1()
{
	UpdateSpacing();

	int n = m_cbTrayNumber.GetCurSel();
	ASSERT(n < m_arrTrayRecord.GetCount() && n >= 0);
	m_nSpacing = m_arrTrayRecord[n].nDx;
	m_nZNumber = m_arrTrayRecord[n].nZNumber;
	m_strCode = m_arrTrayRecord[n].szCode;
	m_nPreviousSel = n;
	UpdateData(FALSE);
}

// 托号改变
void DlgSetTray::UpdateSpacing()
{
	if (m_nPreviousSel == -1)
		return;	// 没有选择

	ASSERT(m_nPreviousSel < m_arrTrayRecord.GetCount());
	UpdateData();
	m_arrTrayRecord[m_nPreviousSel].nDx = m_nSpacing;
	m_arrTrayRecord[m_nPreviousSel].nZNumber = m_nZNumber;
}

// 保存 // ?? 没有改变也保存
void DlgSetTray::OnBnClickedOk()
{
	UpdateSpacing();

	INT_PTR len = m_arrTrayRecord.GetCount();
	for (INT_PTR i = 0; i < len; i++)
	{
		// 保存托号表
		Tray::Record& record = m_arrTrayRecord[i];
		CString strsql;
		strsql.Format(L"UPDATE `tray` SET `dx` = %i, `zNumber` = %i WHERE `id` = %i;\r\n"	// 更新托类型表
			, record.nDx, record.nZNumber, record.id );

		// 加入上一个SQL中，同时更新两个表
		strsql.AppendFormat(L"UPDATE `point` SET `dx` = %i, `xNumber` = `length`/%i,"
			L"`zNumber` = if( `zNumber` > %i, %i, `zNumber`)"	// 如果当前层数大于新层数，设定为新层数，否则不变
			L" WHERE `trayID` = %i;",
			record.nDx, record.nDx, record.nZNumber, record.nZNumber, record.id
		);

		theApp.getDatabase()->Execute(strsql);	//?? 异常

		theApp.m_tray.setRecord(&record);
	}

	ASSERT((m_nPreviousSel & 0xffff) == m_nPreviousSel);	// 高16位没有数
	m_psp->changeTray(m_arrTrayRecord[m_nPreviousSel].id);	// 20190629 改变端点垛号

	AfxMessageBox(L"修改完成，请清空库区，然后重启软件生效。");
	EndDialog(IDOK);
}
